package com.ftg.learn.service.Impl;

import com.ftg.learn.enity.SysUser;
import com.ftg.learn.mapper.UserMapper;
import com.ftg.learn.service.IUserService;
import com.ftg.learn.util.ResultJson;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;


/**
 * @author wangty
 * @date 2022/10/20
 */
@Service
public class IUserImpl implements IUserService {

    @Resource
    UserMapper userMapper;

    /**
     * 系统登陆
     * @param u_account 账号
     * @param u_password 密码
     * @return
     */
    @Override
    public ResultJson loginForUser(String u_account, String u_password) {
        SysUser userInfo = userMapper.loginForUser(u_account,u_password);
        if (Objects.isNull(userInfo)){
            return ResultJson.FAILURE();
        }
        return ResultJson.OK(userInfo);
    }

    /**
     * 判断当前给定的u_id具有哪些角色，以及角色具有全部权限
     * @param u_id 用户编号
     * @return
     */
    @Override
    public ResultJson showPrivilegeWithUid(Integer u_id) {
        return ResultJson.OK(userMapper.showPrivilegeWithUid(u_id));
    }

    /**
     * 创建所有信息
     * @param sysUser User实例
     * @return
     */
    @Override
    public int creatAll(SysUser sysUser, String emp_name, String emp_sex,
                        String emp_code, Integer emp_type, String emp_job,
                        String emp_birthday, Integer clazz_id) {
        Integer b = null;
        Integer abc = userMapper.creatEmployee(emp_name,emp_sex,emp_code,emp_type,emp_job,emp_birthday,clazz_id);
        Map<String,Integer> map = selectEmployee();
        for (Map.Entry<String,Integer> v : map.entrySet()){
            b = v.getValue();
        }
        sysUser.setU_account(emp_name + emp_code);
        sysUser.setU_password("123456");
        sysUser.setE_id(b);

        return userMapper.creatUser(sysUser) + abc;
    }

    /**
     * 用来查询员工表里是否与用户创建的账号重复
     * @return
     */
    @Override
    public Map<String, Integer> selectEmployee() {
        return userMapper.selectEmployee();
    }

    /**
     * 忘记密码，重置密码
     * @param sysUser User实例
     * @return
     */
    @Override
    public int changePassword(SysUser sysUser) {
        return userMapper.changePassword(sysUser);
    }
}
